package info.rflow.graph;

import info.rflow.graph.model.RBlockNode;
import org.netbeans.api.visual.widget.general.IconNodeWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.model.ObjectState;
import org.netbeans.api.visual.laf.LookFeel;

import java.awt.*;


/**
 * @author Holger Brandl
 */
public class RBlockWidget extends IconNodeWidget {


    private RBlockNode rBlockNode;


    enum NodeStatus {READY, EXECUTING, EXECUTED, NOT_READY}


    NodeStatus nodeStatus = NodeStatus.READY;


    public RBlockWidget(Scene scene, RBlockNode node) {
        super(scene);
        this.rBlockNode = node;
    }


    public void setStatus(NodeStatus nodeStatus) {
        this.nodeStatus = nodeStatus;
//        if (rBlockNode.getRBlock().getOutputs().isEmpty())
//            this.nodeStatus = nodeStatus;
//        else
//            this.nodeStatus = nodeStatus.equals(NodeStatus.EXECUTED) ? NodeStatus.READY : nodeStatus;
    }


    @Override
    protected void paintWidget() {
        super.paintWidget();
        Graphics2D graphics2D = getGraphics();

        int hCenter = (int) (getBounds().getWidth() / 2);
        int statusLightWidth = 12;


        boolean hasWD = rBlockNode.getRBlock().getWorkflowModel().getWorkingDirectory() != null;


        // draw a bounding box
        graphics2D.setColor(Color.LIGHT_GRAY);
        graphics2D.drawRoundRect(hCenter - 18 - statusLightWidth, 2, 60, 15, 6, 3);

        graphics2D.setColor(hasWD && nodeStatus.equals(NodeStatus.READY) ? Color.YELLOW : Color.LIGHT_GRAY);
        graphics2D.fillOval(hCenter - 15 - statusLightWidth, 4, statusLightWidth, statusLightWidth);

        graphics2D.setColor(nodeStatus.equals(NodeStatus.EXECUTING) ? Color.RED : Color.LIGHT_GRAY);
        graphics2D.fillOval(hCenter - statusLightWidth / 2, 4, statusLightWidth, statusLightWidth);

//        graphics2D.setColor(nodeStatus.equals(NodeStatus.EXECUTED) ? Color.GREEN : Color.LIGHT_GRAY);
        graphics2D.setColor(hasWD && rBlockNode.getRBlock().isExecuted() && rBlockNode.getRBlock().getOutputs().size() > 0 ? Color.GREEN : Color.LIGHT_GRAY);
        graphics2D.fillOval(hCenter + 15, 4, statusLightWidth, statusLightWidth);
    }


    // This method is necessary to make the complete node to be highlighted on selection
    public void notifyStateChanged(ObjectState previousState, ObjectState state) {
        LookFeel lookFeel = getScene().getLookFeel();
        setBorder(lookFeel.getBorder(state));
        setForeground(lookFeel.getForeground(state));
    }
}
